#ifndef YHAIDA_HTTPSERVER_H
#define YHAIDA_HTTPSERVER_H

#include "../../server/net/TcpServer.h"
#include "../../server/logging/Logging.h"
#include "HttpContext.h"
#include "HttpRequest.h"
#include "HttpResponse.h"
#include <boost/circular_buffer.hpp>
#include <boost/any.hpp>
#include <unordered_set>

namespace yhaida
{
    //前向声明
    class HttpRequest;
    class HttpResponse;
    class HttpServer : Utils::nocapyable
    {
    public:
        typedef std::function<void(const HttpRequest &, HttpResponse *)> HttpCallback;

    public:
        HttpServer(EventLoop *loop,
                   const InetAddress &listenAddr,
                   TcpServer::Option option); //端口复用
        ~HttpServer() = default;
        EventLoop *getLoop() const { return _server.getLoop(); }

        void setHttpCallback(const HttpCallback &cb)
        {
            _httpCallback = cb;
        }
        void setThreadNum(int numThreads)
        {
            _server.setThreadNum(numThreads);
        }
        void start();

    private:
        void onConnection(const TcpConnectionPtr &conn);
        void onMessage(const TcpConnectionPtr &conn,
                       Buffer *buffer,
                       Timestamp receiveTime);
        void onRequest(const TcpConnectionPtr &, const HttpRequest &);

    private:
        TcpServer _server;
        HttpCallback _httpCallback;
    };

    extern std::string readFile(const char *filename);
}
#endif